﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using kim.Common;

namespace kim
{
    public partial class AddorEditImgIndex : kim.UI.ManagePage
    {
        protected string type;
        protected int typeid,fid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            labtitle.Text = "大图";
            this.typeid = KIMRequest.GetInt("typeid", 0);
            this.type = KIMRequest.GetString("type");
            this.fid = KIMRequest.GetInt("fid", 0);
            this.Nid = KIMRequest.GetInt("id", 1);
            if (txtImgUrl_max.Value != "")
                Headpic.Src = txtImgUrl_max.Value;
            if(!IsPostBack) GetRequestValues();
            if (this.fid == 1 && this.typeid == 2)
            { Tr1.Visible = true; jj.Visible = false; }
            if (this.fid == 1 && this.typeid == 1) trBrColour.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (this.type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }
        }

        private void GetRequestValues()
        {
            if (this.type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (this.type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.Show().Delete(Nid);
                if (del)
                    Response.Redirect("ImgIndex.aspx?typeid=" + this.typeid + "& fid=" + this.fid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");

        }
        private bool DoAdd()
        {
            Model.Show mo = new Model.Show();
            mo.Pic = txtImgUrl_max.Value;
            mo.Sort = int.Parse(txt_postion.Value);
            mo.TypeID = this.typeid;
            mo.Title = txtTitle.Text.Trim();
            mo.Wurl=txtUrl.Value;
            mo.ChannelId = this.fid;
            mo.Remark = this.txtRemark.Value.Trim();
            if (this.fid == 1 && this.typeid == 2)
            mo.TitlePic = this.Headpic2.Src;
            if (this.fid == 1 && this.typeid == 1)
                mo.TitlePic = this.txtBrColour.Value;
            BLL.Show bll = new BLL.Show();

            if (bll.Add(mo) > 0)
            {
                Headpic.Src = txtImgUrl_max.Value;
                return true;
                
            }
            else
                return false;
        }
        private bool DoEdit(int id)
        {
            Model.Show mo = new Model.Show();
            mo.Sort =int.Parse(txt_postion.Value);
            mo.Pic = txtImgUrl_max.Value;
            mo.Title = txtTitle.Text.Trim();
            mo.Wurl = txtUrl.Value;
            mo.ChannelId = this.fid;
            mo.ID = id;
            mo.Remark = this.txtRemark.Value.Trim();
            mo.TypeID = this.typeid;
            if (this.fid == 1 && this.typeid == 2)
                mo.TitlePic = this.Headpic2.Src;
            if (this.fid == 1 && this.typeid == 1)
                mo.TitlePic = this.txtBrColour.Value;
            Headpic.Src = txtImgUrl_max.Value;
            return new BLL.Show().Update(mo);
        }
        private void GetEditValue()
        {
            Model.Show model = new BLL.Show().GetModel(Nid);
            txt_postion.Value = model.Sort.ToString();
            Headpic.Src = model.Pic;
            txtTitle.Text = model.Title;
            txtRemark.Value = model.Remark;
            txtUrl.Value = model.Wurl;
            if (this.fid == 1 && this.typeid == 2)
                this.Headpic2.Src= model.TitlePic ;
            if (this.fid == 1 && this.typeid == 1)
                this.txtBrColour.Value = model.TitlePic;
            txtImgUrl_max.Value = model.Pic;
        }
        protected void htn_upload_Click(object sender, EventArgs e)//上传
        {
            string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload1, "../../pic/");
            ViewState["img"] = strpic != "" ? strpic : this.Headpic2.Src.ToString();
            this.Headpic2.Src = ViewState["img"].ToString();
        }
      
    }
}